package com.zhangzy.qm.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.zhangzy.qm.entity.User;
import com.zhangzy.qm.util.Pagination;

public class UserDao extends BaseDao {	
	

	public Pagination<User> queryForPage(Pagination pagination ){
		String queryHql = "from User as user  order by user.id desc";
		String countHql = "select count(*) from User as user ";
		Session session = getSession();
		Query query = session.createQuery(queryHql);
		query.setFirstResult(pagination.getFirstResult());
		query.setMaxResults(pagination.getPageSize());
		Query countQuery = session.createQuery(countHql);
		List list = query.list();
		Long count = (Long) countQuery.uniqueResult();
		pagination.setData(list);
		pagination.setPageCount(count.intValue());
		session.close();
		return pagination;		
	}
	

	public User getUserByParam(String userName ,String password){		
		String queryHql = "from User user where user.userName =:userName and user.password =:password";
		Session session = getSession();
		Query query = session.createQuery(queryHql);
		query.setString("userName", userName);
		query.setString("password", password);
		List list = query.list();
		User user = null ;
		if(list.size() != 0){
			user = (User)list.get(0);
		}
		session.close();
		return user;		
	}
	

	public List<User> findUserByUserName(String userName){
		String hql = "from User user where user.name =:userName";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setString("userName", userName);
		List<User> list = query.list();
		return list;		
	}
	
}
